package com.fpf.springboot.controller;

import java.nio.file.Files;
import java.nio.file.Paths;

import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/faceRegister")
public class FaceRegisterController {
	// 人脸注册
	@RequestMapping(value = "/register/face", method = RequestMethod.POST)
	public Boolean registerFace(@RequestParam(name = "fid") String fid) throws Exception {
		SysFile sysFile = fileService.findById(fid);
		String path = uploadPath + "/" + sysFile.getPath() + "/" + sysFile.getUuidName();
		byte[] bytes = Files.readAllBytes(Paths.get(path));
		Boolean isSuc;
		String image = Base64Utils.encodeToString(bytes);
		isSuc = userService.checkFace(image);
		if (isSuc) {
			isSuc = baiduAiUtil.faceRegister("1", userId, image);
		}
		return isSuc;
	}

	// 人脸更新
	@RequestMapping(value = "/update/face", method = RequestMethod.POST)
	public boolean updateFace(@RequestParam(name = "fid") String fid) throws Exception {
		SysFile sysFile = fileService.findById(fid);
		String path = uploadPath + "/" + sysFile.getPath() + "/" + sysFile.getUuidName();
		byte[] bytes = Files.readAllBytes(Paths.get(path));
		Boolean isSuc;
		String image = Base64Utils.encodeToString(bytes);
		isSuc = userService.checkFace(image);
		if (isSuc) {
			isSuc = baiduAiUtil.faceUpdate("1", userId, image);
		}
		return isSuc;
	}
}
